package com.the4man.framework.util.dialect;

/**
 * Created by lenovo on 2017/12/12.
 */
public class DB2Dialect extends Dialect {
    public DB2Dialect() {
    }

    public String getLimitString(String sql, int offset, int limit) {
        sql = sql.trim();
        if(sql.toLowerCase().endsWith(" with ur")) {
            sql = sql.substring(0, sql.length() - 8);
        }

        if(offset == 0) {
            return sql + " fetch first " + limit + " rows only";
        } else {
            StringBuilder pagingSelect = (new StringBuilder(sql.length() + 200)).append("select * from ( select inner2.*, rownumber() over(order by order of inner2) as rownumber from ( ").append(sql).append("  ) as inner2 ) as inner1 where rownumber > ").append(offset).append(" order by rownumber").append(" fetch first ").append(limit).append(" rows only");
            pagingSelect.append(" with ur");
            return pagingSelect.toString();
        }
    }

    public String getOrderByString(String sql, String sortfiled, String orderby) {
        sql = sql.trim();
        if(sql.toLowerCase().endsWith(" with ur")) {
            sql = sql.substring(0, sql.length() - 8);
        }

        StringBuilder orderSelect = (new StringBuilder(sql.length() + 200)).append("select * from ( ").append(sql).append(" ) ").append(" order by ").append(sortfiled).append(" ").append(orderby);
        orderSelect.append(" with ur");
        return orderSelect.toString();
    }
}
